from lib.hmm import HiddenMarkovModelTrainer
from utils.chars import greek_ch, greeklish_ch
import codecs
from align import align
def demo():
    trainer = HiddenMarkovModelTrainer(states=greek_ch.remove(u''), symbols=greeklish_ch.remove(u''))
#    trainer = HiddenMarkovModelTrainer()    
#    unl_seq = []
#    file = open('greeklish.txt', 'rb')
#    for line in file:
#        sequence = [(t, None) for t in line]
#        unl_seq.append(sequence)
#    
#    model = trainer.train(unlabelled_sequences=unl_seq)
    
    file = codecs.open('greeklish_dict.txt','r','utf-8')

    lab_seq = []
    for line in file:
        [greeklish, greek, rank]=line.split(' - ')
        seq = align(greeklish.lower(), greek.lower())
        if seq:
            lab_seq.append(seq)
    
    model = trainer.train(labelled_sequences=lab_seq)
    
    for test in ['3ypna',]:

        sequence = [(t, None) for t in test]
        print 'Testing with state sequence', test
        try:
            print 'probability =', model.probability(sequence)
            print 'tagging =    ', model.tag(sequence)
            print 'p(tagged) =  ', model.probability(sequence)
            print 'H =          ', model.entropy(sequence)
            print 'H_exh =      ', model._exhaustive_entropy(sequence)
            print 'H(point) =   ', model.point_entropy(sequence)
            print 'H_exh(point)=', model._exhaustive_point_entropy(sequence)
            
            bp = model.best_path(sequence)
            bps = ''.join(bp)
            
            print 'best path', bp, bps.encode('utf-8')

        except KeyError, ke:
            print 'KeyError: ', ke[0].encode('utf-8'), ke


if __name__ == '__main__':
    demo()